const rollup = require("rollup");

const typescript = require("@rollup/plugin-typescript");

const { terser } = require("rollup-plugin-terser");
const pkg = require("../package.json");

const external = [
  ...Object.keys(pkg.dependencies || {}),
  ...Object.keys(pkg.peerDependencies || {}),
];

function build(inputOptions, outputOptions) {
  rollup.rollup(inputOptions).then((bundle) => bundle.write(outputOptions));
}

/**
 * 构建2次
 * - 完整包 cjs
 * - 完整包 es
 */

build(
  {
    input: ["src/index.tsx"],
    plugins: [typescript({}), terser()],
    external,
  },
  {
    sourcemap: true,
    compact: true,
    format: "cjs",
    file: pkg.main,
    exports: "auto",
  }
);

build(
  {
    input: ["src/index.tsx"],
    plugins: [typescript({})],
    external,
  },
  { sourcemap: true, compact: true, format: "es", file: pkg.module }
);
